package com.hhf.cloud.service.impl;

import com.hhf.cloud.apis.AccountFeignApi;
import com.hhf.cloud.apis.StorageFeignApi;
import com.hhf.cloud.entities.DtxOrder;
import com.hhf.cloud.entities.Order;
import com.hhf.cloud.mapper.DtxOrderMapper;
import com.hhf.cloud.service.OrderTccService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @author Daniel-黄海峰
 * @Description
 * @create 2024/3/2 22:16
 */
@Service
@Slf4j
public class OrderTccServiceImpl implements OrderTccService {


    @Resource
    private DtxOrderMapper dtxOrderMapper;

    @Resource
    private AccountFeignApi accountFeignApi;


    @Resource
    private StorageFeignApi storageFeignApi;


    /**
     * TCC模式下的创建订单
     * 订单可以用AT模式，而账户微服务以及库存微服务则使用TCC模式
     * @param order
     */
    @Transactional
    @GlobalTransactional //tcc
    @Override
    public DtxOrder createTCC(DtxOrder order) {
        //xid检查
        String xid = RootContext.getXID();

        //1. 新建订单
        log.info("==================>开始新建订单"+"\t"+"xid_order:" +xid);
        //订单状态status：0：创建中；1：已完结
        order.setStatus(0);
        int result = dtxOrderMapper.insertSelective(order);

        //插入订单成功后获得插入mysql的实体对象
        DtxOrder orderFromDB = null;

        if(result > 0)
        {
            orderFromDB = dtxOrderMapper.selectOne(order);
            log.info("-------> 新建订单成功，orderFromDB info: "+orderFromDB);
            System.out.println();

            //2. 扣减库存
            log.info("-------> 订单微服务开始调用Storage库存，做扣减count");
            storageFeignApi.decreaseTcc(orderFromDB.getProductId(), orderFromDB.getCount());
            log.info("-------> 订单微服务结束调用Storage库存，做扣减完成");
            System.out.println();

            //3. 扣减账号余额
            log.info("-------> 订单微服务开始调用Account账号，做扣减money");
            accountFeignApi.decreaseTcc(orderFromDB.getUserId(),orderFromDB.getMoney());
            log.info("-------> 订单微服务结束调用Account账号，做扣减完成");
            System.out.println();
            //4. 修改订单状态
            //订单状态status：0：创建中；1：已完结
            log.info("-------> 修改订单状态");
            orderFromDB.setStatus(1);

            Example whereCondition = new Example(Order.class);
            Example.Criteria criteria = whereCondition.createCriteria();
            criteria.andEqualTo("userId",orderFromDB.getUserId());
            criteria.andEqualTo("status",0);

            int updateResult = dtxOrderMapper.updateByExampleSelective(orderFromDB, whereCondition);

            log.info("-------> 修改订单状态完成返回值: "+"\t"+updateResult);
            log.info("-------> orderFromDB info: "+orderFromDB);

//            int age = 10/0;
        }
        System.out.println();
        log.info("==================>结束新建订单"+"\t"+"xid_order:" +xid);

        return orderFromDB;
    }
}
